package com.nondo.rdp.finance.endpoint.api.app;

import com.nondo.rdp.auth.endpoint.model.ConvertCall;
import com.nondo.rdp.auth.endpoint.model.Page;
import com.nondo.rdp.auth.endpoint.model.Query;
import com.nondo.rdp.core.common.BeanUtils;
import com.nondo.rdp.core.common.CommUtil;
import com.nondo.rdp.core.model.View;
import com.nondo.rdp.finance.domain.Activity;
import com.nondo.rdp.finance.model.activity.ActivityBean;
import com.nondo.rdp.finance.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController("apiAppActivity")
@RequestMapping("/api/app/activity")
public class ActivityEndpoint {
    @Autowired
    ActivityService activityService;

    @RequestMapping("/info/{id}")
    public View info(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") Long id) {
        View view = new View(20000, "获取数据成功");

        Activity activity = null;
        if (id != null) {
            activity = activityService.findOne(id);
            if (activity.getStartTime() != null) {
                activity.setStringStartTime(CommUtil.dateFormat(activity.getStartTime(), "yyyy-MM-dd"));
            }
            if (activity.getEndTime() != null) {
                activity.setStringEndTime(CommUtil.dateFormat(activity.getEndTime(), "yyyy-MM-dd"));
            }
        } else {
            activity = new Activity();
        }

        view.setData(activity);
        return view;
    }

    @RequestMapping("/list")
    public View<Page<ActivityBean>> list(HttpServletRequest request, HttpServletResponse response, @RequestBody Query<Activity> query) {
        View<Page<ActivityBean>> view = new View(20000, "获取列表成功");

        Activity activity = query.getData();

        Page<Activity> page = activityService.findAll(new Page<Activity>(query.getPage(), query.getSize()), activity);

        Page<ActivityBean> resultPage = page.convertPage(new ConvertCall<Activity, Page<ActivityBean>>() {
            @Override
            public Page<ActivityBean> execute(Page<Activity> srcPage) {
                Page<ActivityBean> ActivityBeanPage = new Page<ActivityBean>(srcPage.getPageNumber(), srcPage.getPageSize(), srcPage.getTotalPages(), srcPage.getTotalElements());

                //处理数据
                List<ActivityBean> list = new ArrayList<ActivityBean>();
                for (Object item : srcPage.getContent()) {
                    ActivityBean activityBean = new ActivityBean();
                    BeanUtils.copy(item, activityBean);

                    //处理图片
                    activityBean.setMainImg(CommUtil.getFileUrl(activityBean.getMainImg()));
                    //处理时间
                    if (activityBean.getStartTime() != null) {
                        activityBean.setStringStartTime(CommUtil.dateFormat(activityBean.getStartTime(), "yyyy-MM-dd"));
                    }
                    if (activityBean.getEndTime() != null) {
                        activityBean.setStringEndTime(CommUtil.dateFormat(activityBean.getEndTime(), "yyyy-MM-dd"));
                    }
                    //处理内容
                    activityBean.setBody("");

                    //判断活动是否已经过期
                    if (activityBean.getEndTime() != null && activityBean.getValid() == 0 && activityBean.getEndTime().getTime() < new Date().getTime()) {
                        activityBean.setIsExpire(1);
                    } else {
                        activityBean.setIsExpire(0);
                    }

                    list.add(activityBean);
                }
                ActivityBeanPage.setContent(list);

                return ActivityBeanPage;
            }
        });

        view.setData(resultPage);

        return view;
    }
}